package com.evildog.workspace.file.controller;

import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.object.PageQuery;
import java.util.List;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.file.model.BucketPageParam;
import com.evildog.workspace.file.model.BucketParam;
import com.evildog.workspace.file.vo.BucketPageVO;
import com.evildog.workspace.file.vo.BucketVO;
import com.evildog.workspace.file.bo.BucketBO;
import com.evildog.workspace.file.bo.BucketPageBO;
import com.evildog.workspace.file.convert.BucketConvert;
import com.evildog.workspace.file.service.BucketService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 * bucket Controller
    存储桶名
 * </p>
 *
 * @author evildog
 * @since 2020-07-19
 */
@RestController
@RequestMapping("/bucket")
@AllArgsConstructor
@Api(tags = "bucket相关接口")
public class BucketController {
    private BucketService bucketService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody BucketParam bucketParam) {
        return ResultGenerator.status(bucketService.save(BucketConvert.INSTANCE.convert(bucketParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(bucketService.removeById(id));
    }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "批量删除接口")
    public Result<Boolean> delete(@RequestParam("ids") List<Integer> ids) {
        return ResultGenerator.status(bucketService.removeByIds(ids));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@RequestBody BucketParam bucketParam) {
        return ResultGenerator.status(bucketService.updateById(id, BucketConvert.INSTANCE.convert(bucketParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<BucketVO> getById(@PathVariable("id") Long id) {
        BucketBO bucketBO = bucketService.getDetailById(id);
        return ResultGenerator.success(BucketConvert.INSTANCE.convert(bucketBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<BucketPageVO>> getPageList(BucketPageParam bucketPageParam,PageQuery query){
        PagedResult<BucketPageBO> bucketPageBO = bucketService.getPageList(
                        query,
                        QueryConditionUtils.getQueryCondition(bucketPageParam),
                        BucketConvert.INSTANCE.convert(bucketPageParam));
        return ResultGenerator.success(BucketConvert.INSTANCE.convert(bucketPageBO));
    }

}
